﻿@model PermissionViewModel

@{
    ViewData["Title"] = "Create Permission Templates";
    Title = "New Permission Templates";
    var expand = true;
    Layout = Constants.AdminLayoutName;
    var model = Model;
}

 
<link href="/Core.Admin/css/userAuth.css" rel="stylesheet" />

@{
    var modules = ((List<IModule>)ViewBag.Modules);
}
<div class="container">
    <form id="permissionTemplateCreateEditForm" class="form-horizontal" asp-controller="UserAuth" asp-action="CreateEditRoles" method="post">

        <div class="row">
            <div class="form-group">
                <label for="Name">Name:</label>
                <input type="text" class="form-control" asp-for="Name">
            </div>
            <div class="form-group">
                <label for="Group">Group:</label>
                <input type="text" class="form-control" asp-for="Group">
            </div>
            <div class="form-group">
                <label for="Group">Description:</label>
                <input type="text" class="form-control" asp-for="Description">
            </div>
            <div class="form-group">
                <label for="Rank">Rank:</label>
                <input type="number" class="form-control" asp-for="Rank">
            </div>
            <input type="hidden" asp-for="Id" value="@Model.Id" />
        </div>

        <h4>Select Module Menus</h4>
        <div class="menu">

            @for (var i = 0; i < modules.Count; i++)
            {
                var module = modules[i];
                var moduleTitle = module.ModuleTitle;

                var menus = module.Menus;
                var adminMenus = menus
                    .Where(x => x.Type == NetCoreCMS.Framework.Core.Models.ViewModels.Menu.MenuType.Admin)
                    .GroupBy(y => y.DisplayName,
                        (key, g) => new { MenuName = key, Menu = g.FirstOrDefault(), Items = g.SelectMany(x => x.MenuItems).ToList() }
                    ).ToList();

                var siteMenus = menus.Where(x => x.Type == NetCoreCMS.Framework.Core.Models.ViewModels.Menu.MenuType.WebSite)
                    .GroupBy(y => y.DisplayName,
                        (key, g) => new { MenuName = key, Menu = g.FirstOrDefault(), Items = g.SelectMany(z => z.MenuItems).ToList() }
                    ).ToList();

                <div class="accordion">
                    <!-- Module -->
                    <div class="accordion-group">

                        <div class="accordion-heading area">
                            <input class="module-check" id="module-check-@i" data-container-id="area-@i" type="checkbox" asp-for="Modules[i].IsChecked" />
                            <input type="hidden" asp-for="Modules[i].ModuleName" value="@module.ModuleName" />
                            <input type="hidden" asp-for="Modules[i].Name" value="@module.ModuleTitle" />
                            <a class="accordion-toggle collapsed" data-toggle="collapse" aria-expanded="false" href="#area-@i">@moduleTitle Module</a>
                        </div>

                        <div class="accordion-body collapse" id="area-@i" aria-expanded="false" style="height: auto;">
                            <div class="accordion-inner">
                                @if (adminMenus.Count > 0)
                                {
                                    <div class="accordion" id="equipamento-admin-@i">
                                        <div class="accordion-group">
                                            <div class="accordion-heading equipamento">
                                                @{
                                                    var checkedTxt = "";
                                                    if (adminMenus.Count > 0)
                                                    {
                                                        var checkedMenu = Model.Modules[i].AdminMenus.Where(x => x.IsChecked && x.MenuItems.Where(y => y.IsChecked).ToList().Count > 0).Count();
                                                        if (checkedMenu > 0)
                                                        {
                                                            checkedTxt = "checked";
                                                        }
                                                    }
                                                }
                                                <input class="admin-check" type="checkbox" @checkedTxt data-container-id="admin-@i" />
                                                <a class="accordion-toggle" data-parent="#equipamento-admin-@i" data-toggle="collapse" aria-expanded="false" href="#admin-@i">Admin Menus</a>
                                            </div>
                                            <div class="accordion-body collapse" id="admin-@i" aria-expanded="false" style="height:auto;">
                                                <div class="accordion-inner">

                                                    @for (var j = 0; j < adminMenus.Count; j++)
                                                    {
                                                        var menu = adminMenus[j];
                                                        var dn = menu.MenuName;
                                                        var menuItems = menu.Items;

                                                        <div class="accordion" id="menu-admin-@i-@j">
                                                            <div class="accordion-group">
                                                                <div class="accordion-heading ponto">
                                                                    <input class="menu-check" type="checkbox" data-container-id="menuItem-admin-@i-@j" asp-for="Modules[i].AdminMenus[j].IsChecked" />
                                                                    <input type="hidden" asp-for="Modules[i].AdminMenus[j].Id" value="@Model.Modules[i].AdminMenus[j].Id" />
                                                                    <input type="hidden" asp-for="Modules[i].AdminMenus[j].Name" value="@menu.MenuName" />
                                                                    <input type="hidden" asp-for="Modules[i].AdminMenus[j].Type" value="Admin" />
                                                                    <a class="accordion-toggle" data-parent="#menu-admin-@i-@j" data-toggle="collapse" aria-expanded="false" href="#menuItem-admin-@i-@j">@menu.MenuName</a>
                                                                </div>
                                                            </div>
                                                            <div class="accordion-body in collapse" id="menuItem-admin-@i-@j" aria-expanded="false" style="height: auto;">
                                                                <div class="accordion-inner">
                                                                    @for (var k = 0; k < menuItems.Count; k++)
                                                                    {
                                                                        var si = menuItems[k];
                                                                        var action = si.DisplayName;

                                                                        <ul class="nav nav-list" style="padding-left:15px;">
                                                                            <li style="margin-bottom:10px;">
                                                                                <input type="checkbox" asp-for="Modules[i].AdminMenus[j].MenuItems[k].IsChecked" />
                                                                                <input type="hidden" asp-for="Modules[i].AdminMenus[j].MenuItems[k].Name" value="@Model.Modules[i].AdminMenus[j].MenuItems[k].Name" />
                                                                                <input type="hidden" asp-for="Modules[i].AdminMenus[j].MenuItems[k].Id" value="@Model.Modules[i].AdminMenus[j].MenuItems[k].Id" />
                                                                                <input type="hidden" asp-for="Modules[i].AdminMenus[j].MenuItems[k].Controller" value="@Model.Modules[i].AdminMenus[j].MenuItems[k].Controller" />
                                                                                <input type="hidden" asp-for="Modules[i].AdminMenus[j].MenuItems[k].Action" value="@Model.Modules[i].AdminMenus[j].MenuItems[k].Action" />
                                                                                <a href="#" style="display:inline;">
                                                                                    @action
                                                                                </a>
                                                                            </li>
                                                                        </ul>
                                                                    }
                                                                </div>
                                                            </div>
                                                        </div>
                                                    }
                                                </div>

                                            </div>
                                        </div>
                                    </div>
                                }

                                @if (siteMenus.Count > 0)
                                {
                                    <div class="accordion" id="equipamento-website-@i">
                                        <div class="accordion-group">
                                            <div class="accordion-heading equipamento">
                                                @{
                                                    var checkedTxt = "";
                                                    if (adminMenus.Count > 0)
                                                    {
                                                        var checkedMenu = Model.Modules[i].SiteMenus.Where(x => x.IsChecked && x.MenuItems.Where(y => y.IsChecked).ToList().Count > 0).Count();
                                                        if (checkedMenu > 0)
                                                        {
                                                            checkedTxt = "checked";
                                                        }
                                                    }
                                                }
                                                <input class="website-check" type="checkbox" @checkedTxt data-container-id="website-@i" />
                                                <a class="accordion-toggle" data-parent="#equipamento-website-@i" data-toggle="collapse" aria-expanded="false" href="#website-@i">Site Menus</a>
                                            </div>
                                            <div class="accordion-body collapse" id="website-@i" aria-expanded="false" style="height:auto;">
                                                <div class="accordion-inner">

                                                    @for (var j = 0; j < siteMenus.Count; j++)
                                                    {
                                                        var menu = siteMenus[j];
                                                        var dn = menu.MenuName;
                                                        var menuItems = menu.Items;

                                                        <div class="accordion" id="menu-website-@i-@j">

                                                            <div class="accordion-group">
                                                                <div class="accordion-heading ponto">
                                                                    <input type="checkbox" data-container-id="menuItem-website-@i-@j" asp-for="Modules[i].SiteMenus[j].IsChecked" />
                                                                    <input type="hidden" asp-for="Modules[i].SiteMenus[j].Id" value="@Model.Modules[i].SiteMenus[j].Id" />
                                                                    <input type="hidden" asp-for="Modules[i].SiteMenus[j].Name" value="@menu.MenuName" />
                                                                    <input type="hidden" asp-for="Modules[i].SiteMenus[j].Type" value="WebSite" />
                                                                    <a class="accordion-toggle" data-parent="#menu-website-@i-@j" data-toggle="collapse" aria-expanded="false" href="#menuItem-website-@i-@j">@menu.MenuName</a>
                                                                </div>
                                                            </div>
                                                            <div class="accordion-body in collapse" id="menuItem-website-@i-@j" aria-expanded="false" style="height: auto;">
                                                                <div class="accordion-inner">
                                                                    @for (var k = 0; k < menuItems.Count; k++)
                                                                    {
                                                                        var si = menuItems[k];
                                                                        var action = si.DisplayName;

                                                                        <ul class="nav nav-list" style="padding-left:15px;">
                                                                            <li style="margin-bottom:10px;">
                                                                                <input type="checkbox" asp-for="Modules[i].SiteMenus[j].MenuItems[k].IsChecked" />
                                                                                <a href="#" style="display:inline;">@action</a>
                                                                                <input type="hidden" asp-for="Modules[i].SiteMenus[j].MenuItems[k].Name" value="@Model.Modules[i].SiteMenus[j].MenuItems[k].Name" />
                                                                                <input type="hidden" asp-for="Modules[i].SiteMenus[j].MenuItems[k].Id" value="@Model.Modules[i].SiteMenus[j].MenuItems[k].Id" />
                                                                                <input type="hidden" asp-for="Modules[i].SiteMenus[j].MenuItems[k].Controller" value="@Model.Modules[i].SiteMenus[j].MenuItems[k].Controller" />
                                                                                <input type="hidden" asp-for="Modules[i].SiteMenus[j].MenuItems[k].Action" value="@Model.Modules[i].SiteMenus[j].MenuItems[k].Action" />
                                                                            </li>
                                                                        </ul>
                                                                    }

                                                                </div>
                                                            </div>
                                                        </div>
                                                    }
                                                </div>

                                            </div>
                                        </div>
                                    </div>
                                }
                            </div>
                        </div>

                    </div>
                </div>
            }
        </div>
        <div class="form-group">
            <input type="submit" class="btn btn-primary" id="submit" value="Save Template" />
        </div>
    </form>
</div>
 

<script> 
    $(document).ready(function () {

        $(".module-check").on("change", function () { 
            var checkBox = $(this);
            var containerId = checkBox.attr("data-container-id");
            console.log(containerId);                        
            $("#"+containerId).find(':checkbox').each(function () { 
                $(this).prop("checked", checkBox.prop("checked"));
            });            
        });

        $(".website-check").on("change", function () {
            var checkBox = $(this);
            var containerId = checkBox.attr("data-container-id");  
            console.log(containerId);
            $("#" + containerId).find(':checkbox').each(function () {
                $(this).prop("checked", checkBox.prop("checked"));
            });                         
        });

        $(".admin-check").on("change", function () {
            var checkBox = $(this);
            var containerId = checkBox.attr("data-container-id");  
            console.log(containerId);
            $("#" + containerId).find(':checkbox').each(function () {
                $(this).prop("checked", checkBox.prop("checked"));
            });                         
        });

        $(".menu-check").on("change", function () {
            var checkBox = $(this);
            var containerId = checkBox.attr("data-container-id");  
            console.log(containerId);
            $("#" + containerId).find(':checkbox').each(function () {
                $(this).prop("checked", checkBox.prop("checked"));
            });                         
        });
    });
</script>